package com.zhuifeng.jsoup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhuifeng.common.core.utils.snow.GuuidUtil;
import com.zhuifeng.product.domain.Goods;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class JsoupHtml {

    public static List<Goods> getHtml() throws IOException {
        //        网页地址
        String url = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&wq=%E6%89%8B%E6%9C%BA";
//        传进一个URL，和响应超时时间，返回一个类似前端Js的DOM对象
        Document doc = org.jsoup.Jsoup.parse(new URL(url), 30000);
//        打印DOM对象的html
//        第一个doc.html()打印出了网页源代码的所有标签+文本内容
//        System.out.println(doc.html());
//        打印DOM对象的所有text
//        第二个doc.text()只打印出网页的文本内容。
//        System.out.println(doc.text());
//        根据网页集合的Calss属性值
        Element goodsList = doc.getElementById("J_goodsList");
//        商品的div calss属性值
        Elements items = goodsList.getElementsByClass("gl-item");
        List<Goods> goods = new ArrayList<>();

        for (Element item : items) {
            Goods goods1 = new Goods();
            String goodsId = item.attr("data-sku");
            String s = String.valueOf(GuuidUtil.getUUID());
            goods1.setGoodsId(s);
            System.out.println(s);
            String name = item.getElementsByClass("p-name p-name-type-2").get(0)
                    .getElementsByTag("em").get(0)
                    .text();
            System.out.println(name);
            goods1.setGoodsName(name);
            String price = item.getElementsByClass("p-price").get(0)
                    .getElementsByTag("i").get(0)
                    .text();
            System.out.println(price);
            goods1.setGoodsPrice(price);
//            获取商品图片
            String img = item.getElementsByTag("img")
                    .eq(0).attr("data-lazy-img");
//            String img = item.getElementsByClass("p-img").get(0)
//                    .getElementsByTag("img").get(0)
//                    .getElementsByTag("img").attr("source-data-lazy-img");
//            String img = item.select("div[class='p-img']").select("img").attr("source-data-lazy-img");
            System.out.println("https:"+img);
            goods1.setGoodsImg("https:"+img);
            goods.add(goods1);

            //            getAppraise(id,1);
//            findProductByKey("手机",12);
        }
//        ExcelUtils.readFromExcelName("D:\\study\\excel\\jsoup.xlsx",Goods.class,0);
        return goods;
    }

    private static void findProductByKey(String keyword,Integer page) throws IOException {
        for (Integer integer = 1 ; integer <= page * 2 -1 ; integer += 2) {
//            网页地址
            String url = "https://search.jd.com/Search?keyword="+keyword+"&page=" + integer;

            Document doc = org.jsoup.Jsoup.parse(new URL(url), 30000);
//            集合标签名称  J_goodsList
            Element goodsList = doc.getElementById("J_goodsList");
//            遍历本页所有商品
            Elements items = goodsList.getElementsByClass("gl-item");

            for (Element item : items) {
//                获取skuId
                String id = item.attr("data-sku");
                System.out.println(id);
//                获取商品标题
                String name = item.getElementsByClass("p-name p-name-type-2").get(0)
                        .getElementsByTag("em").get(0)
                        .text();
                System.out.println(name);
//                获取商品价格
                String price = item.getElementsByClass("p-price").get(0)
                        .getElementsByTag("i").get(0)
                        .text();
                System.out.println(price);
//                获取商品图片
                String img = item.getElementsByTag("img")
                        .eq(0).attr("data-lazy-img");

                System.out.println("https:"+img);
            }
        }
    }
    /**
     * @param id 商品ID
     * @param page 要拿多少页评价
     * @throws  IOException
     */

    private static void getAppraise(String id,int page) throws IOException {
        for (int i = 0; i < page ; i++) {
            String url = "https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId="
                    + id + "&score=0&sortType=5&page=" + i + "&pageSize=10&isShadowSku=0&fold=1";

            Document parse = org.jsoup.Jsoup.parse(new URL(url), 30000);
            System.out.println(parse.html());

            JSONObject jsonObject = JSON.parseObject(parse.text().substring(parse.text().indexOf("(") + 1, parse.text().lastIndexOf(");")));

            JSONArray comments = jsonObject.getJSONArray("tab-con");

            for (Object comment : comments) {
                JSONObject parse1 = (JSONObject) comment;
                System.out.println(parse1.get("comment-con").toString());
                System.out.println("--------------------------------");
            }
        }
    }
}
